package com.deer.wms.base.system.aop;


import com.alibaba.fastjson.JSONObject;
import com.deer.wms.base.system.util.ParseDictUtils;
import com.deer.wms.common.core.page.TableDataInfo;
import com.deer.wms.common.core.result.Result;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author 申紫微
 * @version 1.0
 * @description
 * @data 2020-10-16 09:05:14
 */
@Aspect
@Component
@Slf4j
public class DictAspect {

    @Pointcut("execution(public * com.deer.wms.base.system.web..*.*(..))")
    public void exeService(){
        
    }

    @Around("exeService()")
    public Object doAround(ProceedingJoinPoint pjp) throws Throwable {
        long time1 = System.currentTimeMillis();
        Object result = pjp.proceed();
        long time2 = System.currentTimeMillis();
        log.debug("获取JSON数据 耗时：" + (time2 - time1) + "ms");
        long start = System.currentTimeMillis();
        this.parseDictText(result);
        long end = System.currentTimeMillis();
        log.debug("解析注入JSON数据  耗时" + (end - start) + "ms");
        return result;
    }

    private void parseDictText(Object result) {
        if (Objects.isNull(result)){
            return;
        }
        if (result instanceof TableDataInfo){
            TableDataInfo resTable = (TableDataInfo) result;
            List list = resTable.getRows();
            if (null == list || list.size() == 0) {
                return;
            }
            List<JSONObject> items = new ArrayList<>();
            resTable.setRows(items);
            for (Object obj : list) {
                JSONObject item = ParseDictUtils.parseDictText(obj);
                items.add(item);
            }
        }
    }
}
